package com.risen.project.excel.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.metadata.CellData;
import com.risen.project.excel.handler.ExcelHeadCheckHandler;
import com.risen.project.excel.handler.RowInvokeHandler;
import org.springframework.util.ObjectUtils;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author taoym
 * @date 2022/3/28 17:33
 * @description 读取并且获取表格内的数据 数据将放到dataList中
 */
public class ReadGetListener<T> extends AbstractReadListener<T> {

    /**
     * 存储数据的集合
     */
    private final List<T> dataList = new LinkedList<>();

    private RowInvokeHandler<T> rowInvokeHandler;

    @Override
    public void invokeHead(Map<Integer, CellData> headMap, AnalysisContext context) {
        // 调用父类的表头校验方法
        super.invokeHead(headMap, context);
    }

    @Override
    public void invoke(T data, AnalysisContext context) {
        if (ObjectUtils.isEmpty(rowInvokeHandler)) {
            dataList.add(data);
        } else {
            rowInvokeHandler.invoke(dataList, data, context);
        }
    }

    public ReadGetListener() {

    }

    public ReadGetListener(ExcelHeadCheckHandler headCheckHandler, Class<T> tClass) {
        super.setCheckHandler(headCheckHandler);
        super.settClass(tClass);
    }

    public ReadGetListener(ExcelHeadCheckHandler headCheckHandler, RowInvokeHandler<T> rowInvokeHandler, Class<T> tClass) {
        super.setCheckHandler(headCheckHandler);
        this.rowInvokeHandler = rowInvokeHandler;
        super.settClass(tClass);
    }

    public List<T> getDataList() {
        return dataList;
    }
}
